package com.kaiwukj.android.ufamily.mvp.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.kaiwukj.android.ufamily.utils.location.LocationUtils;

public class LocationService extends Service {


    private boolean isSuccess = false;
    private String lastLatitude = "loading...";
    private String lastLongitude = "loading...";
    private String latitude = "loading...";
    private String longitude = "loading...";
    private String country = "loading...";
    private String locality = "loading...";
    private String street = "loading...";

    private OnGetLocationListener mOnGetLocationListener = null;

    private LocationUtils.OnLocationChangeListener mOnLocationChangeListener = new LocationUtils.OnLocationChangeListener() {

        @Override
        public void getLastKnownLocation(Location location) {
            lastLatitude = String.valueOf(location.getLatitude());
            lastLongitude = String.valueOf(location.getLongitude());
            if (mOnGetLocationListener != null) {
                mOnGetLocationListener.getLocation(lastLatitude, lastLongitude, latitude, longitude, country, locality, street);
            }
        }

        @Override
        public void onLocationChanged(Location location) {
            latitude = String.valueOf(location.getLatitude());
            longitude = String.valueOf(location.getLongitude());
           // mOnGetLocationListener.getLocation(lastLatitude, lastLongitude, latitude, longitude, country, locality, street);
            country = LocationUtils.getCountryName(java.lang.Double.parseDouble(latitude), java.lang.Double.parseDouble(longitude));
            locality = LocationUtils.getLocality(java.lang.Double.parseDouble(latitude), java.lang.Double.parseDouble(longitude));
            street = LocationUtils.getStreet(java.lang.Double.parseDouble(latitude), java.lang.Double.parseDouble(longitude));
            if (mOnGetLocationListener != null) {
                mOnGetLocationListener.getLocation(lastLatitude, lastLongitude, latitude, longitude, country, locality, street);
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };


    public void setOnGetLocationListener(OnGetLocationListener onGetLocationListener) {
        mOnGetLocationListener = onGetLocationListener;
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(() -> {
            Looper.prepare();
            isSuccess = LocationUtils.register(0, 0, mOnLocationChangeListener);
            if (isSuccess) ToastUtils.showShort("init success");
            Looper.loop();
        }).start();
    }

    @SuppressLint("MissingPermission")
    public void reLocation() {
        LocationUtils.refresh(0, 0);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new LocationBinder(this);
    }


    public static class LocationBinder extends Binder {
        LocationService service;

        LocationBinder(LocationService service) {
            this.service = service;
        }

        public LocationService getService() {
            return service;
        }
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onDestroy() {
        LocationUtils.unregister();
        mOnGetLocationListener = null;
        super.onDestroy();
    }

    /**
     * 获取位置监听器
     */
    public interface OnGetLocationListener {
        void getLocation(String lastLatitude, String lastLongitude,
                         String latitude, String longitude,
                         String country, String locality, String street);
    }
}
